<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Picturehome extends CI_Controller {

/**
     * Index action 
     * @author tien.nguyen
     */
	public function index($cateId = null, $page = 1){	
        //instance model
        $this->load->model('Picturehomes', '', true);
        
        $page = empty($page) ? 0 : $page;
        //load data by model 
        $data['Pictures'] = $this->Picturehomes->getListProduct($cateId, $page);
        $data['CateId'] = $cateId;
		$this->load->view('picturehome/index', $data); 
	}
    
    /**
     * Show product 2
     **/
    public function index2($cateId = null, $page = 1){	
        //instance model
        $this->load->model('Products', '', true);
        
        $page = empty($page) ? 0 : $page;
        //load data by model 
        $data['Products'] = $this->Products->getListProduct($cateId, $page);
        $data['CateId'] = $cateId;
		$this->load->view('product/index', $data);
	}
    
    /**
     * edit save config id
     * @author tien.nguyen
     */
    public function edit($productId){
        $res = false;
        $this->load->model('Picturehomes', '', true);
        if($this->input->post()){
            $data = $this->input->post();
            
            //upload file
            if($_FILES['PictureLink']['name'] != ''){
                $resUpload = $this->_uploadFile('PictureLink');
                if(!is_null($resUpload)){       
                    $data['PictureLink'] = $resUpload['file_name'];
                    if($this->Picturehomes->edit($data, $productId)){
                        redirect(base_url().'picturehome.html');          
                    }    
                }
            }else{
                if($this->Picturehomes->edit($data, $productId)){
                    redirect(base_url().'picturehome.html');          
                }    
            }
        }        
        $data['Pictures'] = $this->Picturehomes->getPicture($productId);
        $this->load->view('picturehome/edit', $data);       
    }
    
	/**
     * upload file 
     * @author tien.nguyen
     */
    private function _uploadFile($fileName){
       $config['upload_path'] = $this->config->item('base_upload').'/pictures';;
       $config['allowed_types'] = 'gif|png|jpg';
       $config['max_size'] = '1000';
       $config['max_width'] =  '3000';
       $config['max_height'] = '3000';
       
       $this->load->library('upload', $config);
       if(!$this->upload->do_upload($fileName)){
            $this->session->set_flashdata('message', $this->upload->error_msg);
            return null;
       }else{
            return $this->upload->data();
       }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */